package com.woniuxy.merchant.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.commons.util.WebUtil;
import com.woniu.dto.CompanyContactDto;
import com.woniu.dto.CompanyDto;
import com.woniu.form.UpdateCompanyForm;
import com.woniu.zsd.commons.core.exception.ZsdException;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.merchant.client.PermissionClient;
import com.woniu.zsd.merchant.form.Permission;
import com.woniuxy.merchant.entity.Company;
import com.woniuxy.merchant.mapper.CompanyMapper;
import com.woniuxy.merchant.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公司详情表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private PermissionClient permissionClient;
    @Override
    public CompanyDto adminFindCompany() {
        // 1. 查询公司信息（这里以 id=1 为例）
        Company company = companyMapper.selectById(1);
        if (company == null) {
            return null; // 或者抛出异常，根据业务决定
        }

        // 2. 构建 DTO 并复制基本属性
        CompanyDto companyDto = new CompanyDto();
        BeanUtil.copyProperties(company, companyDto);

        // 3. 处理公司联系方式（英文逗号分隔）
        String contactStr = company.getCompanyContact(); // e.g., "123456789,987654321"
        List<CompanyContactDto> contactList = new ArrayList<>();

        if (StrUtil.isNotBlank(contactStr)) {
            List<String> phones = StrUtil.split(contactStr, ',');
            for (String phone : phones) {
                CompanyContactDto contactDto = new CompanyContactDto();
                contactDto.setName("电话"); // 可根据需求设置类型
                contactDto.setPhone(phone.trim());
                contactList.add(contactDto);
            }
        }

        // 4. 填充到 DTO 并返回
        companyDto.setCompanyContacts(contactList);
        return companyDto;
    }

    @Override
    public void updateCompany(UpdateCompanyForm updateCompanyForm) {
        Result<Permission> userInfo = permissionClient.selInfoByToken(WebUtil.getRequest().getHeader("token"));
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", updateCompanyForm.getId());
        Company company = companyMapper.selectOne(queryWrapper);
        if (company == null) {
            throw new ZsdException("公司不存在",500);
        }
        String companyContactJson = JSONUtil.toJsonStr(updateCompanyForm.getCompanyContact());

        Company info = new Company();
        BeanUtil.copyProperties(updateCompanyForm, info);
        info.setCompanyContact(companyContactJson);
        info.setUpdateUserId(userInfo.getData().getId());
        info.setUpdateUserName(userInfo.getData().getName());
        companyMapper.updateById(info);


    }
}
